//
//  MainVC.m
//  testTableView
//
//  Created by LYJ on 2019/9/19.
//  Copyright © 2019 LYJ. All rights reserved.
//

#import "MainVC.h"
#import "Masonry.h"
#import "Configuration.h"
#import "MainListCell.h"
#import "MainListCell+configure.h"

//#import "testTableView-Swift.h"

@interface MainVC ()<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong) UITableView *homeTableView;
@property (nonatomic, strong) NSMutableArray *docArr;

@end

@implementation MainVC

- (void)loadView {
    [super loadView];
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.homeTableView];
    [self viewsLayout];
    [self fetchData];
    
//    TestShow *test = [[TestShow alloc] init];
//    [test showWordWithStr:@"^_^ hello Swift"];
}

#pragma mark - dataManager


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.docArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        cell = [[MainListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId" ];
    }
    [cell configureCellWithInfoDic:self.docArr[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH_PRO(50);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    MainListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"click %ld row",indexPath.row);
}

#pragma mark - event response
- (void)buyBtnClick
{
    
}

- (void)popVC
{
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - private methods
- (void)fetchData{
    
    self.docArr = [NSMutableArray arrayWithArray:@[@{@"userName":@"zhang",
                                                     @"introduce":@"123242144124"},
                                                   @{@"userName":@"zhang1",
                                                     @"introduce":@"123242144124123412324214412312312313131231313123141412515234234242342424242342424242424224242323423242342424242242424242342424241232421441231231231313123131312314141251523423424234242424234242424242422424232342324234242424224242424234242424123242144123123123131312313131231414125152342342423424242423424242424242242423234232423424242422424242423424242"},
                                                   @{@"userName":@"zhang2",
                                                     @"introduce":@"15453412324214412412324214412312312313131231313123141414"},
                                                   @{@"userName":@"zhang3",
                                                     @"introduce":@"146323242144124"},
                                                   @{@"userName":@"zhang4",
                                                     @"introduce":@"74574123242144123123123131312313131231414125152342342423424242423424242424242242423234232423424242422424242423424242422124"},
                                                   ]];
    [self.homeTableView reloadData];
}


#pragma mark - getters and setters
- (UITableView *)homeTableView
{
    if (!_homeTableView)
    {
        _homeTableView = [[UITableView alloc]
                          initWithFrame:CGRectZero
                          style:UITableViewStylePlain];
        _homeTableView.translatesAutoresizingMaskIntoConstraints = NO;
        _homeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _homeTableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.94 alpha:1];
        _homeTableView.delegate = self;
        _homeTableView.dataSource = self;
    }
    return _homeTableView;
}

#pragma mark - layout
- (void)viewsLayout{
    [self.homeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.left.mas_equalTo(self.view);
    }];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
